// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information.
// Website: https://admin.blazor.zone



using System.Diagnostics.CodeAnalysis;
using BootstrapBlazor.Components;
using Identity.Interface;
using Identity.Models;
using Microsoft.AspNetCore.Components;
using Microsoft.EntityFrameworkCore;
using Identity.Extensions;

namespace Identity.Account.Setting;
public partial class RolesManage: ComponentBase 
{
    [Inject]
    [NotNull]
    private DialogService? DialogService { get; set; }

    [Inject]
    [NotNull]
    private ToastService? ToastService { get; set; }
  


  
    [Inject]
    [NotNull]
    private IRole? RoleService { get; set; }


    [Inject]
    [NotNull]
    private IUser? UserService { get; set; }

    [Inject]
    [NotNull]
    private INavigation? NavigationService { get; set; }


  private Task<QueryData<Role>> OnQueryAsync(QueryPageOptions options)
  {
      var menus = RoleService.GetAll();
     

      // 处理模糊查询
      if (options.Searches.Any())
      {
          menus = menus.Where(options.Searches.GetFilterFunc<Role>(FilterLogic.Or)).ToList();
      }

      //  处理 Filter 高级搜索
      if (options.CustomerSearches.Any() || options.Filters.Any())
      {
          menus = menus.Where(options.CustomerSearches.Concat(options.Filters).GetFilterFunc<Role>()).ToList();
      }



      return Task.FromResult(new QueryData<Role>()
      {
          IsFiltered = true,
          IsSearch = true,
          IsSorted = true,
          Items = menus
      });
  }



   private async Task OnAssignmentUsers(Role role)
    {
        var users = UserService.GetAll().ToSelectedItemList();
        var values = UserService.GetUsersByRoleId(role.Id);

        await DialogService.ShowAssignmentDialog($"分配用户 - 当前角色: {role.Name}", users, values, () =>
        {
            var ret = UserService.SaveUsersByRoleId(role.Id, values);
            return Task.FromResult(ret);
        }, ToastService);
    }
    private async Task OnAssignmentMenus(Role role)
    {
        var menus = NavigationService.GetAllMenus();
        var values = NavigationService.GetMenusByRoleId(role.Id);
        await DialogService.ShowNavigationDialog($"分配菜单 - 当前角色: {role.Name}", menus, values, items =>
        {
            var ret = NavigationService.SaveMenusByRoleId(role.Id, items);
            return Task.FromResult(ret);
        });
    }

   
}
